package com.ge.energy.pgs.myfleet.code.user;

import java.io.IOException;


import java.util.ArrayList;

import java.util.List;
import java.util.Map.Entry;

import com.ge.energy.pgs.myfleet.code.factory.ChatRoomFactory;
import com.ge.energy.pgs.myfleet.code.factory.MyServletFactory;
import com.ge.energy.pgs.myfleet.code.factory.OfflineMessageFactory;
import com.ge.energy.pgs.myfleet.code.vo.ChatRoomInfo;
import com.ge.energy.pgs.myfleet.code.vo.ChatRoomMessage;
import com.ge.energy.pgs.myfleet.code.vo.Contact;

import com.ge.energy.pgs.myfleet.code.vo.Contacts;
import com.ge.energy.pgs.myfleet.code.vo.OfflineMessage;
import com.google.gson.Gson;

public class BedoBasicImpl {
	public Contact myInfo = null;
	public String roomId = null;
//	private boolean isLogin = false;
	public Contacts myContacts = null; 
	public String password = null;
	public Gson gson = new Gson();

	
	public void $service_enterRoom(String roomId){
		System.out.print(roomId);
		if(roomId.equals("") || this.password == null)
			return;
		
		if(ChatRoomFactory.getChatRooms().containsKey(roomId)){
			this.roomId = roomId;
			
			System.out.println(this.myInfo.getContactName());
			
			ChatRoomFactory.getChatRooms().get(roomId).getChatRoomContacts().put(this.myInfo.getContactId(),this.myInfo);
			
			
		System.out.println("sizesize:" + ChatRoomFactory.getChatRooms().get(roomId).getChatRoomContacts().size());	;
			

			String backData = gson.toJson(ChatRoomFactory.getChatRooms().get(roomId));
			System.out.print(backData);
			try {
				if(backData != null){
					System.out.println("=========================="+ backData);
					MyServletFactory.getSockets().get(this.myInfo.getContactId()).getConn().sendMessage("{\"flag\":\"$client_enterRoom\",\"status\":\"success\",\"backvalue\":"+ backData +"}");
					service_room_user_changed("in");
				}else{
					MyServletFactory.getSockets().get(this.myInfo.getContactId()).getConn().sendMessage("{\"flag\":\"$client_enterRoom\",\"status\":\"进入聊天室失败\",\"backvalue\":\"\"}");
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	public void $service_leaveRoom(String roomId){
		if(this.roomId == null  || this.password == null)
			return;
		String myId = this.myInfo.getContactId();
		ChatRoomFactory.getChatRooms().get(this.roomId).getChatRoomContacts().remove(myId);
		service_room_user_changed("out");
		this.roomId = null;
	}
	
	public void service_room_user_changed(String in_out){
		if(this.roomId == null  || this.password == null)
			return;
		if(in_out.equals("in")){
			String myInfoStr = gson.toJson(this.myInfo);
			String message = "{\"flag\":\"$client_room_user_in\",\"status\":\"success\",\"backvalue\":"+myInfoStr+"}";
			service_room_message_sender(message);
		}else if(in_out.equals("out")){
			String message = "{\"flag\":\"$client_room_user_out\",\"status\":\"success\",\"backvalue\":"+this.myInfo.getContactId()+"}";
			service_room_message_sender(message);
		}
	}
	
	private void service_room_message_sender(String message){
		ChatRoomInfo roomInfo = ChatRoomFactory.getChatRooms().get(this.roomId);
		for( Entry<String, Contact> entry: roomInfo.getChatRoomContacts().entrySet()){
			
			Contact contact = entry.getValue();
			if(contact.getContactId().equals(this.myInfo.getContactId()))
				continue;
			try {
				MyServletFactory.getSockets().get(contact.getContactId()).getConn().sendMessage(message);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public void $service_room_chat(String message){
		if(this.roomId == null  || this.password == null || message.equals(""))
			return;
		
		
		String backMessage = "{\"flag\":\"$client_room_chat\",\"status\":\""+this.myInfo.getContactId()+"\",\"backvalue\":\""+message+"\"}";
		
		
		ChatRoomMessage roomMg = new ChatRoomMessage();
		roomMg.setContact(this.myInfo);
		roomMg.setMessage(message);
		roomMg.setDate("");
		
		ChatRoomFactory.getChatRooms().get(this.roomId).getChatRoomMessages().add(roomMg);
		
		service_room_message_sender(backMessage);
	}
	
	public void $service_contact_chat(String message){
		if(this.password == null || message.equals(""))
			return;
		
		int index = message.indexOf("&");
		String receiveId = message.substring(0,index);
		String msg = message.substring(index+1);
		
		service_contact_message_sender(receiveId,msg);
	}
	
	public void $service_offline_message(String message){
		if(this.password == null)
			return;
		String userId = this.myInfo.getContactId();
		if(OfflineMessageFactory.offlineMsgs.containsKey(userId)){
			String offlineMsgsStr = gson.toJson(OfflineMessageFactory.offlineMsgs.get(userId));
			try {
				MyServletFactory.getSockets().get(userId).getConn().sendMessage("{\"flag\":\"$client_offline_message\",\"status\":\"success\",\"backvalue\":"+ offlineMsgsStr +"}");
			} catch (IOException e) {
				e.printStackTrace();
			}
			
			OfflineMessageFactory.offlineMsgs.remove(userId);
		}
	}
	
	private void service_contact_message_sender(String receiveId,String message){
		
		String backMessage = "{\"flag\":\"$client_contact_chat\",\"status\":\""+this.myInfo.getContactId()+"\",\"backvalue\":\""+message+"\"}";
		
		if(MyServletFactory.getSockets().containsKey(receiveId)){
			try {
				MyServletFactory.getSockets().get(receiveId).getConn().sendMessage(backMessage);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}else{
			addOfflineMsg(this.myInfo.getContactId(),receiveId,message);
		}
	}
	
	public void  addOfflineMsg(String myId,String receiveId,String msg){


		List<OfflineMessage> oflms = null;
		if(OfflineMessageFactory.offlineMsgs.containsKey(receiveId)){
		oflms = OfflineMessageFactory.offlineMsgs.get(receiveId);
		}else{
			oflms = new ArrayList<OfflineMessage>();
			OfflineMessageFactory.offlineMsgs.put(receiveId, oflms);
		}
		
		if(oflms.size() < OfflineMessageFactory.offlineMsgMaxNum){
			OfflineMessage offlineMsg = new OfflineMessage();
			offlineMsg.setDate("11:30");
			offlineMsg.setMessage(msg);
			offlineMsg.setSenderFlag("S");
			offlineMsg.setSenderId(myId);
			offlineMsg.setSnederInfo("");
			oflms.add(offlineMsg);
		}
		
	}
	
	
}
